package com.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.base.BasePageReq;
import com.base.BasePageRes;
import com.base.BaseServiceImpl;
import com.entity.Artical;
import com.entity.Rss;
import com.exception.ServiceException;
import com.repository.RssRepository;
import com.req.RssPageReq;
import com.req.RssReq;
import com.res.ArticalRes;
import com.res.RssListRes;
import com.res.RssRes;
import com.service.RssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class RssServiceImpl extends BaseServiceImpl<RssReq,RssRes, RssRepository, Rss> implements RssService {
    @Autowired
    private RssRepository rssRepository;
    @Autowired
    private ArticalServiceImpl articalService;
    @Override
    public RssRes saveRss(RssReq req) {
        return this.handle(req);
    }
    @Override
    public RssRes del(RssReq req) {
        QueryWrapper<Rss> wrapper = new QueryWrapper<>();
        wrapper.lambda().and(Wrapper->Wrapper.eq(Rss::getUserId,req.getUserId()).eq(Rss::getChannelId,req.getChannelId()));
        List<Rss> Rsss = this.list(wrapper);
        for (Rss Rss : Rsss) {
            req.setId(Rss.getId());
            super.del(req);
        }

        return null;
    }

    @Override
    public RssRes query(RssReq req) {
        QueryWrapper<Rss> wrapper = new QueryWrapper<>();
        wrapper.lambda().and(Wrapper->Wrapper.eq(Rss::getUserId,req.getUserId()).eq(Rss::getChannelId,req.getChannelId()));
        Rss Rss = this.getOne(wrapper);
        RssRes RssRes = new RssRes();
        BeanUtil.copyProperties(Rss,RssRes);
        return RssRes;
    }

    /**
     *
     * @param pageReq
     * @return
     */
    @Override
    public BasePageRes queryPage(RssPageReq pageReq) {
        if (StringUtils.isEmpty(pageReq.getPage())) {
            throw new ServiceException("请传入page");
        }
        if (StringUtils.isEmpty(pageReq.getSize())) {
            throw new ServiceException("请传入size");
        }
        if(!StringUtils.isEmpty(pageReq.getDes())) {
            pageReq.setDes("%" + pageReq.getDes() + "%");
        }
        Page<RssRes> pages = new Page<>(pageReq.getPage(), pageReq.getSize());
        //获取页头
        IPage<RssRes> resPage = rssRepository.searchPage(pages, pageReq);
        //获取文文章
        List<RssRes> records = resPage.getRecords();
        for (RssRes record : records) {
               List<ArticalRes> articalResList =  rssRepository.searchArtical(record.getChannelId(),pageReq.getTop(),pageReq.getDes());
               record.setArticalResList(articalResList);
        }
        BasePageRes<RssRes> basePageRes = this.convertIpage(resPage, new RssRes());

        //进行降序排序
        List<RssRes> collectList = basePageRes.getList().stream().sorted((t1, t2) -> {
            return t2.getCounts() - t1.getCounts();
        }).collect(Collectors.toList());
        basePageRes.setList(collectList);
        return basePageRes;
    }

    @Override
    public BasePageRes detailPage(RssPageReq pageReq) {
        if (StringUtils.isEmpty(pageReq.getPage())) {
            throw new ServiceException("请传入page");
        }
        if (StringUtils.isEmpty(pageReq.getSize())) {
            throw new ServiceException("请传入size");
        }

        QueryWrapper<Artical> wrapper = new QueryWrapper<>();
        //wrapper.lambda().and(Wrapper-> Wrapper.eq(Artical::getChannelId,pageReq.getChannelId()));
        if (!StringUtils.isEmpty(pageReq.getDes())) {
            wrapper.lambda().and(Wrapper->Wrapper.like(Artical::getTitle, pageReq.getDes()).or().like(Artical::getDescre, pageReq.getDes()));
        }
        wrapper.lambda().orderByDesc(Artical::getCrDate);
        IPage<Artical> pages = new Page<>(pageReq.getPage(), pageReq.getSize());
        IPage<Artical> articalIPage = articalService.page(pages, wrapper);
        BasePageRes pageRes = articalService.convertIpage(articalIPage,  new ArticalRes());
        return pageRes;
    }
}
